package com.qiyue.admin.controller.base;

import com.qiyue.admin.biz.base.MenuBiz;
import com.qiyue.admin.biz.base.UserBiz;
import com.qiyue.admin.constant.AdminCommonConstant;
import com.qiyue.admin.entity.base.Menu;
import com.qiyue.admin.vo.AuthorityMenuTree;
import com.qiyue.admin.vo.MenuTree;
import com.qiyue.common.controller.BaseController;
import com.qiyue.common.utils.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by y747718944 on 2018/2/8
 */
@RestController
@RequestMapping("menu")
@Api(description = "菜单")
public class MenuController  extends BaseController<MenuBiz,Menu>{
    @Autowired
    private UserBiz userBiz;

    @GetMapping("/list")
    @ApiOperation("模糊查询菜单名")
    public List<Menu> list(String title){
        String filters=null;
        if(StringUtils.isNotBlank(title)){
            filters+="LIKE_title="+title;
        }
        return baseBiz.search(filters);
    }


    @GetMapping("/tree")
    @ApiOperation("模糊查询菜单名")
    public List<MenuTree> getTree(String title){
        String filters=null;
        if(StringUtils.isNotBlank(title)){
            filters+="LIKE_title="+title;
        }
        return  getMenuTree(baseBiz.search(filters), AdminCommonConstant.ROOT);
    }

    @GetMapping("/system")
    public List<Menu> getSystem(){
        return baseBiz.search("EQ_parentId="+AdminCommonConstant.ROOT);
    }

    @GetMapping("/menuTree")
    @ApiOperation("根据parentId 查询菜单集")
    public List<MenuTree> listMenu(Long parentId){
        try {
            if (parentId == null) {
                parentId = this.getSystem().get(0).getId();
            }
        } catch (Exception e) {
            return new ArrayList<MenuTree>();
        }
//        List<MenuTree> trees = new ArrayList<MenuTree>();
//        MenuTree node = null;
        Menu parent = baseBiz.findOne(parentId);
        Map<String,Object> params=new HashMap<>();
        if (StringUtils.isNotBlank(parent.getPath())){
            params.put("LIKE_path",parent.getPath());
        }
            //注意是不等于
            params.put("NOTEQ_id",parent.getId());
        return getMenuTree(baseBiz.findList(params), parent.getId());
    }

    @GetMapping("/authorityTree")
    @ApiOperation("获取 菜单权限树")
    public List<AuthorityMenuTree> listAuthortiyMenu(){
        List<AuthorityMenuTree> trees = new ArrayList<AuthorityMenuTree>();
        baseBiz.findAll().forEach( menu -> {
            AuthorityMenuTree  node = new AuthorityMenuTree();
            node.setText(menu.getTitle());
            BeanUtils.copyProperties(menu, node);
            trees.add(node);
        });
        return TreeUtil.bulid(trees, AdminCommonConstant.ROOT);
    }

    @GetMapping("/user/authorityTree")
    @ApiOperation("")
    public List<MenuTree> listUserAuthortiyMenu(Long parentId){
          Long userId = userBiz.getUserByAccout(getCurrentUserAccount()).getId();
        try {
            if (parentId == null) {
                parentId = this.getSystem().get(0).getId();
            }
        } catch (Exception e) {
            return new ArrayList<MenuTree>();
        }
        return  getMenuTree(baseBiz.getUserAuthorityMenuByUserId(userId),parentId);
    }

    @GetMapping("/user/system")
    public List<Menu> listUserAuthortiySystem(){
        Long userId = userBiz.getUserByAccout(getCurrentUserAccount()).getId();
        return baseBiz.getUserAuthoritySystemByUserId(userId);
    }



    /**
     * 遍历menu tree
     * @param menus
     * @param root
     * @return
     */
    private List<MenuTree> getMenuTree(List<Menu> menus,Long root) {
        List<MenuTree> trees = new ArrayList<MenuTree>();
        MenuTree node = null;
        for (Menu menu : menus) {
            node = new MenuTree();
            BeanUtils.copyProperties(menu, node);
            node.setLabel(menu.getTitle());
            trees.add(node);
        }
        return TreeUtil.bulid(trees,root) ;
    }

}
